import Vue from 'vue'
import Router from 'vue-router'

// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading

Vue.use(Router)

/* Layout */
import Layout from '../views/layout/Layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: 'dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'example', noCache: true, affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '/students',
    component: Layout,
    // meta: { title: '学生信息', icon: 'form' },
    children: [
      {
        path: 'index',
        name: 'students',
        component: () => import('@/views/students'),
        meta: { title: '学生列表' }
      }
    ]
  },
  {
    path: '/sponsors',
    component: Layout,
    // meta: { title: '资助人', icon: 'form' },
    children: [
      {
        path: 'index',
        name: 'sponsors',
        component: () => import('@/views/sponsors'),
        meta: { title: '资助人列表' }
      }
    ]
  },
  {
    path: '/volunteers',
    component: Layout,
    // meta: { title: '志愿者', icon: 'form' },
    children: [
      {
        path: 'index',
        name: 'volunteers',
        component: () => import('@/views/volunteers'),
        meta: { title: '志愿者列表' }
      }
    ]
  },
  {
    path: '/fundWork',
    component: Layout,
    redirect: '/fundWork/aidTrack',
    meta: { title: '资助工作', icon: 'form' },
    children: [
      {
        path: 'aidTrack',
        name: 'aidTrack',
        component: () => import('@/views/fundWork/aidTrack'),
        meta: { title: '资助跟踪总表' }
      },
      {
        path: 'apply',
        name: 'apply',
        component: () => import('@/views/fundWork/apply'),
        meta: { title: '申请和打款' }
      },
      {
        path: 'pair',
        name: 'pair',
        component: () => import('@/views/fundWork/pair'),
        meta: { title: '结对和捐款' }
      },
      {
        path: 'feedback',
        name: 'feedback',
        component: () => import('@/views/fundWork/feedback'),
        meta: { title: '资助人反馈' }
      }
    ]
  },

  {
    path: '/masterData',
    component: Layout,
    redirect: '/masterData/projects',
    meta: { title: '主数据', icon: 'form' },
    children: [
      {
        path: 'projects',
        name: 'projects',
        component: () => import('@/views/masterData/projects'),
        meta: { title: '项目管理' }
      },
      {
        path: 'schoolYears',
        name: 'schoolYears',
        component: () => import('@/views/masterData/schoolYears'),
        meta: { title: '学年管理' }
      },
      {
        path: 'semesters',
        name: 'semesters',
        component: () => import('@/views/masterData/semesters'),
        meta: { title: '学期管理' }
      },
      {
        path: 'grades',
        name: 'grades',
        component: () => import('@/views/masterData/grades'),
        meta: { title: '年级管理' }
      },
      {
        path: 'bankAccounts',
        name: 'bankAccounts',
        component: () => import('@/views/masterData/bankAccounts'),
        meta: { title: '银行账户' }
      },
      {
        path: 'accountingSubjects',
        name: 'accountingSubjects',
        component: () => import('@/views/masterData/accountingSubjects'),
        meta: { title: '会计科目' }
      }
    ]
  },

  // {
  //   path: '/system',
  //   component: Layout,
  //   meta: { title: '系统管理', icon: 'setting' },
  //   children: [
  //     {
  //       path: 'user',
  //       name: 'user',
  //       component: () => import('@/views/system/userManage'),
  //       meta: { title: '用户管理' }
  //     },
  //     {
  //       path: 'module',
  //       name: 'module',
  //       component: () => import('@/views/system/module'),
  //       meta: { title: '模块管理' }
  //     },
  //     {
  //       path: 'menuOperate',
  //       name: 'menuOperate',
  //       component: () => import('@/views/system/menuOperate'),
  //       meta: { title: '菜单操作配置' }
  //     },
  //     {
  //       path: 'userGroupOperate',
  //       name: 'userGroupOperate',
  //       component: () => import('@/views/system/userGroupOperate'),
  //       meta: { title: '用户组权限配置' }
  //     },
  //     {
  //       path: 'dic',
  //       name: 'dic',
  //       component: () => import('@/views/system/dic'),
  //       meta: { title: '字典管理' }
  //     }
  //   ]
  // },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
